package com.wjqy.service.impl;

import com.wjqy.entity.*;
import com.wjqy.mapper.ScenicMapper;
import com.wjqy.queryVo.ScenicVo;
import com.wjqy.service.ScenicService;
import com.wjqy.utils.CaculateDistance;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ScenicServiceImpl implements ScenicService {

    @Autowired
    private ScenicMapper scenicMapper;


    //根据用户的经纬度获取最近的景区的id
    public Integer chooseScenic(double x, double y) {
        List<Local> allScenicLocal = scenicMapper.findAllScenicLocal();
        GlobalCoordinates source = new GlobalCoordinates(y, x);
        Map<Double,Integer> localMap = new HashMap<Double,Integer>();
        for (Local local : allScenicLocal) {
            GlobalCoordinates target = new GlobalCoordinates(local.getDimension(), local.getLongitude());
            double distanceMeter = CaculateDistance.getDistanceMeter(Ellipsoid.Sphere, source, target);
            localMap.put(distanceMeter,local.getScenic_id());
        }
        Collection<Double> distance = localMap.keySet();
        Double min = Collections.min(distance);
        Integer id = localMap.get(min);
        System.out.println("id----------------->>>>"+id);
        return id;
    }

    //根据id返回对应的景区数据
    public ScenicVo showScenic(int id) {
        ScenicVo scenicVo = scenicMapper.showScenic(id);
        List<String> banner = scenicMapper.findBanner(id);
        scenicVo.setPics(banner);
        // Scenic scenic = scenicVo.getScenic();
        // scenicVo.setScenic(scenic);
        return scenicVo;
    }

    public List<Local> findAllScenicLocal() {
        List<Local> allScenicLocal = scenicMapper.findAllScenicLocal();
        return allScenicLocal;
    }

    public List<Ticket> findScenicTicket(int id) {
        List<Ticket> tickets = scenicMapper.findScenicTicket(id);
        return tickets;

    }

    public List<Comment> findAllComment(int id) {
        List<Comment> allComment = scenicMapper.findAllComment(id);
        for (Comment comment : allComment) {
            if (comment.getPics()!=null){
                comment.setPicList(comment.getPics().split(";"));
            }
        }
        return allComment;
    }
}
